package com.cyrus.rabbitm.listener;

import com.rabbitmq.client.Channel;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.core.MessageListener;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.amqp.rabbit.listener.api.ChannelAwareMessageListener;
import org.springframework.stereotype.Component;

/**
 * Consumer ACK
 */
@Component
public class RabbitMQListener1 implements ChannelAwareMessageListener {

    /**
     * 手动签收
     *      1、listener:simple:acknowledge-mode: manual #手动签收
     *      2、监听器继承ChannelAwareMessageListener
     *      3、消息成功：
     */
    //@RabbitListener(queues = "boot_confirm")
    @Override
    public void onMessage(Message message, Channel channel) throws Exception {
        Thread.sleep(2000);
        long deliveryTag = message.getMessageProperties().getDeliveryTag();
        try {
//            System.out.println(new String(message.getBody()));
            System.out.println("进行业务处理");
            /**
             * void basicAck(long deliveryTag, boolean multiple) throws IOException;
             * deliveryTag：收到的消息的标签
             * multiple：是否接收多条消息
             */
            int i = 3 / 0;
            channel.basicAck(deliveryTag,true);

        } catch (Exception e) {
            /**
             *void basicNack(long deliveryTag, boolean multiple, boolean requeue)
             *
             * requeue:重回队列，若发送失败，消息重回queue，broker重新发送给consumer
             */
            System.out.println("业务处理失败");
            channel.basicNack(deliveryTag,true,true);
        }
    }
}
